package com.sprd.simple.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.sprd.simple.adapter.ThirdPageAdapter;
import com.sprd.simple.launcher.R;
import com.sprd.simple.model.IconInfo;
import com.sprd.simple.model.LauncherGridView;

import java.util.ArrayList;

/**
 * Created by SPRD on 2016/7/19.
 */
public class ThirdWorkspaceFragment extends LauncherFragment implements AdapterView.OnItemClickListener {
    private static final String TAG = "ThirdWorkspaceFragment";

    private final static int ICON_CALENDAR = 0;
    private final static int ICON_CALCULATOR = 1;
    private final static int ICON_CLOCK = 2;
    private final static int ICON_FMRADIO = 3;
    private final static int ICON_GALLERY = 4;
    private final static int ICON_SETTINGS = 5;
    private Context mContext;

    public ThirdWorkspaceFragment() {
        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_third_pager, container, false);
        mContext = getContext();
        initView(view);
        return view;
    }

    private void initView(View view) {
        appNameForTTS = new String[]{
                getResources().getString(R.string.calendar_name),
                getResources().getString(R.string.calculator_name),
                getResources().getString(R.string.clock_name),
                getResources().getString(R.string.fm_name),
                getResources().getString(R.string.gallery_name),
                getResources().getString(R.string.settings_name)};

        mGridView = (LauncherGridView) view.findViewById(R.id.third_grid_view);
        mAdapter = new ThirdPageAdapter(getData(), mContext, appNameForTTS, mGridView);
        mGridView.setAdapter(mAdapter);
        mGridView.setSelector(getContext().getResources().getDrawable(R.color.transparent));
        mGridView.setOnItemClickListener(this);
    }

    private ArrayList getData() {
        ArrayList<IconInfo> list = new ArrayList<>();

        IconInfo calendar = new IconInfo();
        calendar.setIconName(getResources().getString(R.string.calendar_name));
        calendar.setIconImage(R.drawable.app_calender);
        calendar.setPackageName(getResources().getString(R.string.calendar_package));
        list.add(calendar);

        IconInfo calculator = new IconInfo();
        calculator.setIconName(getResources().getString(R.string.calculator_name));
        calculator.setIconImage(R.drawable.app_calculator);
        calculator.setPackageName(getResources().getString(R.string.calculator_package));
        list.add(calculator);

        IconInfo clock = new IconInfo();
        clock.setIconName(getResources().getString(R.string.clock_name));
        clock.setIconImage(R.drawable.app_clock);
        clock.setPackageName(getResources().getString(R.string.clock_package));
        list.add(clock);

        IconInfo fmRadio = new IconInfo();
        fmRadio.setIconName(getResources().getString(R.string.fm_name));
        fmRadio.setIconImage(R.drawable.app_fm_radio);
        fmRadio.setPackageName(getResources().getString(R.string.fm_package));
        list.add(fmRadio);

        IconInfo gallery = new IconInfo();
        gallery.setIconName(getResources().getString(R.string.gallery_name));
        gallery.setIconImage(R.drawable.app_gallery);
        gallery.setPackageName(getResources().getString(R.string.gallery_package));
        list.add(gallery);

        IconInfo settings = new IconInfo();
        settings.setIconName(getResources().getString(R.string.settings_name));
        settings.setIconImage(R.drawable.app_settings);
        settings.setPackageName(getResources().getString(R.string.settings_package));
        list.add(settings);

        return list;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        switch (position) {
            case ICON_CALENDAR:
                intent.setClassName(getResources().getString(R.string.calendar_package),
                        getResources().getString(R.string.calendar_activity));
                startActivity(intent);
                break;

            case ICON_CALCULATOR:
                intent.setClassName(getResources().getString(R.string.calculator_package),
                        getResources().getString(R.string.calculator_activity));
                startActivity(intent);
                break;

            case ICON_CLOCK:
                intent.setClassName("com.android.deskclock", "com.android.deskclock.AlarmsMainActivity");
                startActivity(intent);
                break;

            case ICON_FMRADIO:
                // com.huawei.android.FMRadio/.FMRadioMainActivity
                intent.setClassName("com.huawei.android.FMRadio", "com.huawei.android.FMRadio.FMRadioMainActivity");
                startActivity(intent);
                break;

            case ICON_GALLERY:
                // com.android.gallery3d/com.huawei.gallery.app.GalleryMain
                intent.setClassName("com.android.gallery3d", "com.huawei.gallery.app.GalleryMain");
                startActivity(intent);
                break;

            case ICON_SETTINGS:
                // com.android.settings/com.android.settings.HWSettings
                intent.setClassName("com.android.settings", "com.android.settings.HWSettings");
                startActivity(intent);
                break;

            default:
                break;
        }

    }

}
